package com.tronsis.distribution.activity.goods;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ProductBean;
import com.tronsis.distribution.bean.ProductDetailBean;
import com.tronsis.distribution.biz.factory.ProductBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.GlideApp;
import com.tronsis.library.util.DisplayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * allen: 显示库存数量
 */
public class GoodsDetailActivity extends ToolBarBaseActivity {

    @BindView(R.id.banner)
    BGABanner mBanner;
    @BindView(R.id.tv_goods_title)
    TextView mTvGoodsTitle;
    @BindView(R.id.tv_contribute)
    TextView mTvContribute;
    @BindView(R.id.tv_freight)
    TextView mTvFreight;
    @BindView(R.id.tv_amount)
    TextView mTvAmount;
    @BindView(R.id.tv_detail)
    TextView mTvDetail;

    private List<String> mBannerBeanList = new ArrayList<>();
    private ArrayList<ImageItem> mImageItems = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.goods_detail));

        ProductBean productBean = (ProductBean) getIntent().getSerializableExtra("ProductBean");
        setView(productBean);

        initBanner();
    }

    private void setView(ProductBean productBean) {
        if (productBean == null) {
            return;
        }

        mTvGoodsTitle.setText(productBean.getName());
        if(TextUtils.isEmpty(productBean.getStock())){
            mTvAmount.setVisibility(View.GONE);
        }else{
            mTvAmount.setText("库存数量："+productBean.getStock());
        }
        getProductDetail(productBean.getId());
    }

    private void initBanner() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mBanner.getLayoutParams();
        params.height = DisplayUtil.getInstance(mContext).getScreenWidth() * 8 / 15;
        mBanner.setLayoutParams(params);
        mBanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String bean, final int position) {
                GlideApp.with(mContext).load(Url.FILE_DOWNLOAD + bean).placeholder(R.drawable.home_banner).error(R.drawable
                        .home_banner).centerCrop().into(itemView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intentPreview = new Intent(GoodsDetailActivity.this, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageItems);
                        startActivity(intentPreview);
                    }
                });

            }
        });

    }


    private void getProductDetail(String productId) {
        try {
            ProductBizFactory.createBiz().getProductDetail(this, productId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ProductDetailBean productDetailBean = (ProductDetailBean) data;
                    setDetailView(productDetailBean);

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void setDetailView(ProductDetailBean productDetailBean) {
        if (productDetailBean == null) {
            return;
        }
        mTvGoodsTitle.setText(productDetailBean.getName());
        mTvContribute.setText(getString(R.string.format_contribute, productDetailBean.getContribute()));
        mTvFreight.setText(getString(R.string.format_freight, productDetailBean.getFreight()));
        mTvDetail.setText(productDetailBean.getDetail());


        String fid = productDetailBean.getSlideFid();
        if (fid != null) {
            String[] split = fid.split(",");
            mBannerBeanList.clear();
            mBannerBeanList.addAll(Arrays.asList(split));
            mImageItems.clear();
            for (String string : mBannerBeanList) {
                ImageItem imageItem = new ImageItem();
                imageItem.url = Url.FILE_DOWNLOAD + string;
                mImageItems.add(imageItem);
            }
            mBanner.setData(mBannerBeanList, null);
        }
    }

}
